<?php

namespace app\user\controller\mall\shop;

use app\user\controller\mall\Base;
/**
 * 店铺设置
 *
 * @icon fa fa-circle-o
 */
class Set extends Base
{
    
    /**
     * Set模型对象
     * @var \app\common\model\mall\shop\Set
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\mall\shop\Set;

    }

    /**
     * 店铺设置
     */
    public function index(){
        $row = $this->model->where("shop_id",(int)cookie('shop_id'))->find();

        $user_ids = $this->getDataLimitUserIds();
        if (is_array($user_ids))
        {
            if (!in_array($row[$this->dataLimitField], $user_ids))
            {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");
            if ($params)
            {
                try
                {
                    //将按钮中的状态参数加入记录中
                    $params['status'] = input('status',0);

                    //是否采用模型验证
                    if ($this->modelValidate)
                    {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false)
                    {
                        $this->success();
                    }
                    else
                    {
                        $this->error($row->getError());
                    }
                }
                catch (\think\exception\PDOException $e)
                {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);

        return $this->view->fetch($row?"edit":"add");
    }

}
